﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Select测试
{
    class Program
    {
        static void Main(string[] args)
        {
            DataTable dt = new DataTable("T1");
            dt.Columns.Add("Id", typeof(int));
            dt.Columns["Id"].AutoIncrement = true;
            dt.Columns["Id"].AutoIncrementSeed = 1;
            dt.Columns.Add("Name", typeof(string));
            dt.Columns.Add("Genter", typeof(string));
            dt.Columns.Add("Birthday", typeof(DateTime));
            dt.Columns.Add("StuCode", typeof(string));
            Random rd = new Random();

            //添加第一列
            DataRow row1 = dt.NewRow();
            row1["Name"] = "张三";
            row1["Genter"] = "男";
            row1["Birthday"] = DateTime.Now.AddYears(-19);
            row1["StuCode"] = "2017" + rd.Next(1000, 10000);
            dt.Rows.Add(row1);

            DataRow row2 = dt.NewRow();
            row2["Name"] = "张三";
            row2["Genter"] = "男";
            row2["Birthday"] = DateTime.Now.AddYears(-20);
            row2["StuCode"] = "2017" + rd.Next(1000, 10000);
            dt.Rows.Add(row2);

            DataRow row3 = dt.NewRow();
            row3["Name"] = "小龚";
            row3["Genter"] = "女";
            row3["Birthday"] = DateTime.Now.AddYears(-21);
            row3["StuCode"] = "2017" + rd.Next(1000, 10000);
            dt.Rows.Add(row3);

            DataRow row5 = dt.NewRow();
            row5["Name"] = "小龚";
            row5["Genter"] = "女";
            row5["Birthday"] = DateTime.Now.AddYears(-21);
            row5["StuCode"] = "2017" + rd.Next(1000, 10000);
            dt.Rows.Add(row5);

            DataRow row4 = dt.NewRow();
            row4["Name"] = "小红";
            row4["Genter"] = "女";
            row4["Birthday"] = DateTime.Now.AddYears(-20);
            row4["StuCode"] = "2017" + rd.Next(1000, 10000);
            dt.Rows.Add(row4);



            var list = from dts in dt.AsEnumerable()
                       group dts by new
                       {
                           Name = dts.Field<string>("Name"),
                           Genter = dts.Field<string>("Genter")
                       } into g
                       where g.Count() > 1
                       select new
                       {
                           g.Key.Name,
                           g.Key.Genter
                       };
            string error = "人员编号{0}下的{1}产品线有重复数据,请检查数据。\r\n";
            string errors = string.Empty;
            foreach (var item in list)
            {
                errors += string.Format(error, item.Name, item.Genter);
            }


            Console.WriteLine(errors);
            Console.ReadKey();

        }
    }
}
